package controllayer;

import java.util.ArrayList;

import dblayer.DBConnection;
import dblayer.DBEmployee;
import dblayer.IFDBEmployee;
import modellayer.City;
import modellayer.Employee;

public class CtrEmployee {

	private IFDBEmployee dbObj;
	
	private CtrCity ctrCity;
	
	public CtrEmployee(){
		dbObj = new DBEmployee();
		ctrCity = new CtrCity();
	}
	
	public ArrayList<Employee> getAllEmployees(){
		ArrayList<Employee> list = new ArrayList<Employee>();
		list = dbObj.getAllEmployee();
		return list;
	}
	
	public Employee searchByID(int id){
		return dbObj.searchByID(id);
	}
	
	public Employee searchByName(String name){
		return dbObj.searchByName(name);
	}
	
	public Employee searchByPhone(int phone){
		return dbObj.searchByPhone(phone);
	}
	
	//
	
	public int updateEmployee(int id, String name, int phone, String address, City city, String email){
		Employee emp = new Employee();
		
		emp.setID(id);
		emp.setName(name);
		emp.setPhone(phone);
		emp.setAddress(address);
		emp.setCity(city);
		emp.setEmail(email);
		
		return dbObj.updateEmployee(emp);
	}
	
	public void createEmployee(String name, String address, int zipCode, String city, int phone, String email){
		Employee emp = new Employee();
		
		ctrCity.createCity(zipCode, city);
		
		emp.setName(name);
		emp.setPhone(phone);
		emp.setAddress(address);
		emp.setCity(ctrCity.searchByZipCode(zipCode));
		emp.setEmail(email);
		
		try{
			DBConnection.startTransaction();
			dbObj.insertEmployee(emp);
			DBConnection.commitTransaction();
		}
		catch(Exception e){
			DBConnection.rollbackTransaction();
		}
	}
	
	public int deleteEmployee(int id){
		return dbObj.deleteEmployee(id);
	}
}
